package com.lagou.homework.stage02.module01.Task01.app;

import com.lagou.homework.stage02.module01.Task01.entity.Employee;
import com.lagou.homework.stage02.module01.Task01.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class EmpTest {
    public static void main(String[] args) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

            System.out.println("查询所有的员工信息 (不包含没有部门的员工)");
            String sql01 = "select * from employee e, dept d where e.did = d.id and did is not null";
            List<Employee> list01=qr.query(sql01, new BeanListHandler<Employee>(Employee.class));
            for(Employee emp : list01){
                System.out.println(emp);
            }

            System.out.println("查询每个员工的 姓名, 薪资 和 所属部门名称)");
            String sql02 = "select e.name, e.salary, d.deptname from employee e,dept d where e.did=d.id";
            List<Object[]> query = qr.query(sql02, new ArrayListHandler());
            for (Object[] objects : query) {
                System.out.println(Arrays.toString(objects));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
